home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / util / cdity / ModeProSrc.lha / Prefs / New / MPPSelectList.c < prev    next >
C/C++ Source or Header  |  1996-06-12  |  14KB  |  495 lines

  1. #include <debug.h>
  2. #include <intuition/intuition.h>
  3. #include <graphics/text.h>
  4. #include "mpp.h"
  5. #include <intuition/intuitionbase.h>
  6. #include <exec/execbase.h>
  7. #include <clib/extras_protos.h>
  8. #include <stdio.h>
  9. #include <math.h>
  10. #include <dos/dosextens.h>
  11.  
  12. #define LGD_LIST    0
  13. #define LGD_OK      1
  14. #define LGD_CANCEL  2
  15.  
  16. struct ListRequester
  17. {
  18.   struct Window     *lr_Window;
  19.   APTR               lr_VisualInfo;
  20.   struct LG_Control *lr_GadControl;
  21.   struct List       *lr_List;
  22.   BOOL   lr_RetVal;
  23. };
  24.  
  25. void CreateDefaultModeList(struct List *List);
  26. struct ModeNode *CreateModeNode(ULONG ID);
  27. //void FreeDefaultModeList(struct List *List);
  28.  
  29. struct Gadget  *MakeListGadget  (struct List *List);
  30.          void  RemListGadget    (void);
  31.  
  32. extern struct Screen *Scr;
  33. extern struct ExecBase *SysBase;
  34. extern struct IntuitionBase *IntuitionBase;
  35. extern struct TextAttr *TAttr;
  36. extern struct Window *Win;
  37.  
  38. BOOL SelectName(struct DefaultNode *DN)
  39. {
  40.   STRPTR name,namework;
  41.   struct List namelist,*tasklist;
  42.   struct Node *namenode,*execnode;
  43.   struct Screen *scr;
  44.   struct Task   *task;
  45.   LONG l,
  46.        ok=TRUE,
  47.        lock,
  48.        item;
  49.   BOOL rv=FALSE;
  50.   ULONG type;
  51.   UBYTE cliname[256];
  52.   
  53.   if(DN)
  54.   {
  55.     NewList(&namelist);
  56.     if(DN->Type<3)
  57.     {
  58.       switch(DN->Type)
  59.       {
  60.         case 0:
  61.           // Create a list of program names
  62.           Disable();
  63.           tasklist=&SysBase->TaskReady;
  64.           for(l=0;l<2;l++)
  65.           {
  66.             task=(struct Task *)tasklist[l].lh_Head;
  67.  
  68.             while(task->tc_Node.ln_Succ)
  69.             {
  70.               execnode=(struct Node *)task;
  71.               name=execnode->ln_Name;
  72.               type=execnode->ln_Type;
  73.               
  74.               if(type==NT_PROCESS)
  75.               {
  76.                 struct Process *proc;
  77.                 struct CommandLineInterface *cli;
  78.                 
  79.                 //kprintf("Process ");
  80.                 
  81.                 proc=(struct Process *)task;
  82.                 
  83.                 if(proc->pr_CLI)
  84.                 {
  85.                   cli=(struct CommandLineInterface *)BADDR(proc->pr_CLI);
  86.                   namework=(STRPTR)BADDR(cli->cli_CommandName);
  87.                   if(namework[0])
  88.                   {
  89.                     strncpy(cliname,&namework[1],namework[0]);
  90.                     cliname[namework[0]]=0;
  91.                     name=cliname;
  92.                   }
  93.                 }
  94.               }
  95.               //else
  96.                 //kprintf("Task    ");
  97.                 
  98.               //kprintf("%s %ld\n",name,type);  
  99.               
  100.               if(name)
  101.               {
  102.                 if(namenode=AllocNameNode(name))
  103.                 {
  104.                   namenode->ln_Type=type;
  105.                   namenode->ln_Pri=execnode->ln_Pri;
  106.                   EnqueueName(&namelist,namenode);
  107.                   
  108.                 }
  109.                 //else
  110.                 //  ok=FALSE;
  111.               }
  112.               task=(struct Task *)task->tc_Node.ln_Succ;
  113.             }
  114.           }
  115.           Enable();
  116.           break;
  117.         case 1:          
  118.           // Create a list of screens
  119.           lock=LockIBase(0);
  120.           scr=IntuitionBase->FirstScreen;
  121.           while(scr && ok)
  122.           {
  123.             if(scr->Title)
  124.             {
  125.               if(namenode=AllocNameNode(scr->Title))
  126.                 EnqueueName(&namelist,namenode);
  127.               else
  128.                 ok=FALSE;
  129.             }
  130.             scr=scr->NextScreen;
  131.           }
  132.           UnlockIBase(lock);
  133.           break;
  134.         case 2:
  135.           CreateDefaultModeList(&namelist);
  136.           break;
  137.       }
  138.       if((item=SelectReq(&namelist,GetString(MSG_SELWIN_PROGRAMS + DN->Type))) >=0)
  139.       {
  140.         STRPTR dummy;
  141.         
  142.         namenode=namelist.lh_Head;
  143.         while(item)
  144.         {
  145.           namenode=namenode->ln_Succ;
  146.           item--;
  147.         }
  148.         dummy=DN->Def_Node.ln_Name;
  149.         // The old name will be freed with FreeNameNodes()
  150.         DN->Def_Node.ln_Name=namenode->ln_Name;
  151.         namenode->ln_Name=dummy;
  152.         if(DN->Type==2)
  153.           DN->OrigDisplayID=((struct ModeNode *)namenode)->mn_DisplayID;
  154.         rv=TRUE;
  155.       }
  156.       FreeNameNodes(&namelist);
  157.     }
  158.     /*
  159.     else // dn->Type==2
  160.     {
  161.       if(DN->Type==2)
  162.       {
  163.         STRPTR scrname;
  164.         
  165.         if(SelectScreenMode(Win,DN,TRUE))
  166.         {
  167.           if(scrname=GetModeName(DN->OrigDisplayID))
  168.           {
  169.             FreeVec(DN->Def_Node.ln_Name);
  170.             DN->Def_Node.ln_Name=scrname;
  171.             rv=TRUE;
  172.           }
  173.         }
  174.       }
  175.     }*/
  176.   }
  177.   return(rv);
  178. }
  179.  
  180. struct Node *AllocNameNode(STRPTR Name)
  181. {
  182.   struct Node *n;
  183.   LONG l;
  184.   
  185.   if(n=AllocVec(sizeof(struct Node),MEMF_PUBLIC|MEMF_NO_EXPUNGE|MEMF_CLEAR))
  186.   {
  187.     n->ln_Pri=0;
  188.     l=strlen(Name);
  189.     if(n->ln_Name=AllocVec(l+1,MEMF_PUBLIC|MEMF_NO_EXPUNGE))
  190.     {
  191.       strncpy(n->ln_Name,Name,l);
  192.       n->ln_Name[l]=0;
  193.       return(n);
  194.     }
  195.     FreeVec(n);
  196.   }
  197.   return(0);
  198. }
  199.  
  200. void FreeNameNodes(struct List *List)
  201. {
  202.   struct Node *n;
  203.   
  204.   while(n=RemHead(List))
  205.   {
  206.     FreeVec(n->ln_Name);
  207.     FreeVec(n);
  208.   }
  209. }
  210.  
  211. WORD ListWinLeft,ListWinTop,ListWinWidth,ListWinHeight;
  212. BOOL ListFirstTime=TRUE;
  213. APTR ListVI;
  214. ULONG ListGadCount;
  215. struct Window *ListWin;
  216. struct Gadget *ListContext,*ListLV;
  217.  
  218. /* Opens the list requester */
  219. LONG SelectReq(struct List *List, STRPTR WinTitle)
  220. {
  221.   LOMG nodes;
  222.   struct Requester *req;
  223.   LONG rv=-1;
  224.   struct IntuiMessage *imsg;
  225.   BOOL ok=TRUE;
  226.   WORD x1,y1,x2,y2;
  227.   LONG PrevSeconds =0,
  228.        PrevMicros  =0,
  229.        PrevCode    =-1;
  230.   
  231.   if(ListFirstTime)
  232.   {
  233.     ListWinWidth=   Win->Width/2;
  234.     ListWinHeight=  Win->Height-16;
  235.     ListWinLeft=    Win->LeftEdge+Win->Width/2-ListWinWidth/2;
  236.     ListWinTop=     Win->TopEdge+8;
  237.     ListWinWidth=   max(ListWinWidth,50);
  238.     ListWinHeight=  max(ListWinHeight,50);
  239.     ListFirstTime=FALSE;
  240.   }
  241.   
  242.   req=Busy(Win);
  243.   if(ListWin=OpenWindowTags(0,WA_Left,          ListWinLeft,
  244.                               WA_Top,           ListWinTop,
  245.                               WA_Width,         ListWinWidth,
  246.                               WA_Height,        ListWinHeight,
  247.                               WA_MinWidth,      50,
  248.                               WA_MinHeight,     50,
  249.                               WA_MaxWidth,      -1,
  250.                               WA_MaxHeight,     -1,
  251.                               WA_Title,         WinTitle,
  252.                               WA_DragBar,       TRUE,
  253.                               WA_SizeGadget,    TRUE,
  254.                               WA_SizeBBottom,   TRUE,
  255.                               WA_DepthGadget,   TRUE,
  256.                               WA_SimpleRefresh, TRUE    ,
  257.                               WA_CloseGadget,   TRUE,
  258.                               WA_CustomScreen,  Scr,
  259.                               WA_Activate,      TRUE,
  260.                               WA_IDCMP,         IDCMP_REFRESHWINDOW |
  261.                                                 IDCMP_CLOSEWINDOW   |
  262.                                                 IDCMP_NEWSIZE       |
  263.                                                 IDCMP_VANILLAKEY    |
  264.                                                 IDCMP_RAWKEY        |
  265.                                                 LISTVIEWIDCMP,
  266.                               TAG_DONE))
  267.   {
  268.     x1=ListWin->BorderLeft;
  269.     y1=ListWin->BorderTop;
  270.     if(ListVI=GetVisualInfo(ListWin->WScreen,0))
  271.     {
  272.       if(MakeListGadget(List))
  273.       {
  274.         nodes=CountNodes(List);
  275.         while(ok)
  276.         {
  277.           WaitPort(ListWin->UserPort);
  278.           while(imsg=GT_GetIMsg(ListWin->UserPort))
  279.           {
  280.             switch(imsg->Class)
  281.             {
  282.               case IDCMP_REFRESHWINDOW:
  283.                 GT_BeginRefresh(ListWin);
  284.                 GT_EndRefresh(ListWin,TRUE);
  285.                 break;
  286.               case IDCMP_CLOSEWINDOW:
  287.                 ok=FALSE;
  288.                 rv=-1;
  289.                 break;
  290.               case IDCMP_GADGETUP:
  291.                 if(DoubleClick(PrevSeconds,PrevMicros,imsg->Seconds,imsg->Micros) 
  292.                    && PrevCode==imsg->Code)
  293.                 {
  294.                   ok=FALSE;
  295.                   rv=imsg->Code;
  296.                 }
  297.                 else
  298.                 {
  299.                   PrevSeconds=imsg->Seconds;
  300.                   PrevMicros =imsg->Micros;
  301.                   PrevCode   =imsg->Code;
  302.                 }
  303.                 break;
  304.               case IDCMP_NEWSIZE:
  305.                 x2=ListWin->Width -ListWin->BorderRight-1;
  306.                 y2=ListWin->Height-ListWin->BorderBottom-1;
  307.                 RemListGadget();
  308.                 if(x2>x1 && y2>y1)
  309.                   EraseRect(ListWin->RPort,x1,y1,x2,y2);
  310.                 RefreshWindowFrame(ListWin);
  311.                 if(!MakeListGadget(List))
  312.                 { 
  313.                   ok=FALSE;
  314.                   rv=-1;
  315.                 }
  316.                 else
  317.                 {
  318.                   ListWinWidth=   ListWin->Width;
  319.                   ListWinHeight=  ListWin->Height;
  320.                   ListWinLeft=    ListWin->LeftEdge;
  321.                   ListWinTop=     ListWin->TopEdge;
  322.                 }
  323.                 break;
  324.               case IDCMP_RAWKEY:
  325.                 switch(imsg->Code)
  326.                 {
  327.                   case CURSORUP:
  328.                     PrevCode--;
  329.                     if(PrevCode<0)
  330.                       PrevCode=nodes-1;
  331.                     break;
  332.                   case CURSORDOWN:
  333.                     PrevCode++;
  334.                     if(PrevCode>=nodes)
  335.                       PrevCode=0;
  336.                     break;
  337.                 }
  338.                 GT_SetGadgetAttrs(
  339.                     
  340.             }
  341.             GT_ReplyIMsg(imsg);
  342.           }
  343.         }
  344.         RemListGadget();
  345.       }
  346.       FreeVisualInfo(ListVI);
  347.     }
  348.     CloseWindow(ListWin);
  349.   }
  350.   NotBusy(Win,req);
  351.   return(rv);
  352. }
  353.  
  354. struct Gadget *MakeListGadget(struct List *List)
  355. {
  356.   struct Gadget *gad;
  357.   struct NewGadget listng={0};
  358.  
  359.   listng.ng_VisualInfo=ListVI;
  360.   listng.ng_TextAttr  =TAttr;
  361.   listng.ng_LeftEdge  =ListWin->BorderLeft  +4;
  362.   listng.ng_TopEdge   =ListWin->BorderTop   +4;
  363.   listng.ng_Width     =ListWin->Width-ListWin->BorderLeft-ListWin->BorderRight-8;
  364.   listng.ng_Height    =ListWin->Height-ListWin->BorderTop-ListWin->BorderBottom-8;
  365.   
  366.   gad=CreateContext(&ListContext);
  367.  
  368.   ListLV=gad=CreateGadget(LISTVIEW_KIND, gad, &listng,
  369.                           GTLV_Labels           ,List,
  370.                           GTLV_ShowSelected     ,0,
  371.                           TAG_DONE);
  372.   if(!gad)
  373.   {
  374.     FreeGadgets(ListContext);
  375.     ListContext=0;
  376.     return(0);
  377.   }
  378.   else
  379.   {
  380.     struct Gadget *cgad;
  381.     
  382.     ListGadCount=0;
  383.     cgad=ListContext;
  384.     
  385.     while(cgad)
  386.     {
  387.       ListGadCount++;
  388.       cgad=cgad->NextGadget;
  389.     }
  390.     
  391.     AddGList(ListWin,ListContext,-1,-1,0);
  392.     RefreshGList(ListContext,ListWin,0,ListGadCount);
  393.     GT_RefreshWindow(ListWin,NULL);
  394.   }
  395.   return(gad);
  396. }
  397.  
  398. void RemListGadget(void)
  399. {
  400.   if(ListContext)
  401.   {
  402.     RemoveGList(ListWin,ListContext,ListGadCount);
  403.     FreeGadgets(ListContext);
  404.     ListContext=0;
  405.   }
  406. }
  407.  
  408.  
  409. void CreateDefaultModeList(struct List *List)
  410. {
  411.   struct DisplayInfo di;
  412.   struct ModeNode    *mn;
  413.   ULONG previd=MONITOR_ID_MASK,id;
  414.   
  415.   id=NextDisplayInfo(INVALID_ID);
  416.  
  417.   while(id!=INVALID_ID)
  418.   {
  419.     if(previd != (id & MONITOR_ID_MASK))
  420.     { // create an entry for all screen modes.
  421.       previd=(id & MONITOR_ID_MASK);
  422.       if(mn=CreateModeNode(id | ALL_MODES))
  423.         EnqueueName(List,(struct Node *)mn);      
  424.     } 
  425.     
  426.     if(GetDisplayInfoData(0,(UBYTE *)&di,sizeof(di),DTAG_DISP,id))
  427.     if(!(di.PropertyFlags & SPECIAL_FLAGS)) // no HAM, EHB, 2PF etc.
  428.     {
  429.       if(mn=CreateModeNode(id))
  430.       
  431.       if(mn=AllocVec(sizeof(struct ModeNode),MEMF_PUBLIC|MEMF_CLEAR))
  432.       {
  433.         if(mn->mn_Node.ln_Name=GetModeName(id))
  434.           EnqueueName(List,(struct Node *)mn);
  435.       }
  436.     }
  437.     id=NextDisplayInfo(id);
  438.   }
  439. }
  440.  
  441. struct ModeNode *CreateModeNode(ULONG ID)
  442. {
  443.   struct ModeNode *mn;
  444.   
  445.   if(mn=AllocVec(sizeof(struct ModeNode),MEMF_PUBLIC|MEMF_CLEAR))
  446.   {
  447.     if(mn->mn_Node.ln_Name=GetModeName(ID))
  448.     {
  449.       mn->mn_DisplayID=ID;
  450.       return(mn);
  451.     }
  452.     FreeVec(mn);
  453.   }
  454.   return(0);
  455. }
  456.  
  457.  
  458. struct LG_Control *LayoutListGads(struct ListRequester *LReq)
  459. {
  460.   WORD width,height,
  461.   struct LG_Control *lr_control=0;
  462.   //STRPTR ok,cancel;
  463.  
  464.   if(LReq)
  465.   {
  466.     if(LReq->lr_VisualInfo && LReq->lr_Window)
  467.     {
  468.       rp=LReq->lr_Window->RPort; 
  469.  
  470.       width =LReq->lr_Window->Width  - LReq->lr_Window->BorderLeft- LReq->lr_Window->BorderRight  - 8;
  471.       height=LReq->lr_Window->Height - LReq->lr_Window->BorderTop - LReq->lr_Window->BorderBottom - 8;
  472.  
  473.       lr_control=LG_CreateGadgets( LG_VisualInfo        ,LReq->lr_VisualInfo,
  474.                                    LG_UseWindowOffsets  ,PWin,
  475.                                    LG_EraseRemoved      ,FALSE,
  476.                                    LG_OffsetX           ,4,
  477.                                    LG_OffsetY           ,4,
  478.                                    LG_TextAttr          ,TAttr,
  479.                                    
  480.                                    /* List Gadget */
  481.                                    LG_GadgetKind        ,LISTVIEW_KIND,
  482.                                    LG_GadgetID          ,LGD_LIST,
  483.                                    LG_Width             ,width,
  484.                                    LG_Height            ,height,
  485.                                    LG_GadgetTags        ,2,
  486.                                    GTLV_Labels          ,List,
  487.                                    GTLV_ShowSelected    ,0,
  488.                                    LG_CreateGadget      ,0,
  489.                                  
  490.                                    TAG_DONE);
  491.     }
  492.   }
  493.   return(lr_control);
  494. }                                 
  495.